// pages/login/login.js

const app = getApp()
var timer = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    telNum: false,
    tel: "", //手机号
    sms: "", //验证码
    time: 60, //倒计时
    time_status: true, //倒计时60秒
    sma_status: true, //重新获取
    trueSms: "",
    trueTel: "",
    displayNo: true,
    telFlag: false, //验证手机号是否为空
    smsFlag: false // 验证验证码是否为空
  },



  statusBtn() {
    var url = app.globalData.url + "customer/login",
      that = this;
    if (that.data.sms == '') {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return false;
    }
    if (that.data.sms != that.data.trueSms) {
      wx.showToast({
        title: '验证码不正确',
        icon: 'none'
      })
      return false;
    }
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        userTel: that.data.trueTel
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          wx.showToast({
            title: '登录成功',
            icon: 'none'
          })

          app.globalData.userId = res.data.userId
          app.globalData.companyId = res.data.companyId

          wx.setStorageSync('saleUserId', res.data.userId)
          wx.setStorageSync('salecompanyId', res.data.companyId)
          wx.switchTab({
            url: '/pages/index/index',
          })
        }
      },
      err => {
        console.log(err);
      })

  },


  // 验证码
  times() {
    if (this.data.tel == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return false;
    }
    if (!(/^1[3456789]\d{9}$/.test(this.data.tel))) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return false;
    }

    var url = app.globalData.url + "customer/userLogin",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        userTel: that.data.tel
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          if (res.data.flag == "error") {
            wx.showToast({
              title: '该号码不是业务员',
              icon: 'none'
            })
            that.setData({
              time_status: true,
              sma_status: false,
            })
          } else {
            wx.showToast({
              title: '已将验证码发送至你的手机',
              icon: 'none'
            })
            that.setData({
              telNum: false,
              trueSms: res.data.ycode,
              trueTel: that.data.tel,
              time: 60,
              time_status: false,
              sma_status: true,
            })
            timer = setInterval(() => {
              if (that.data.time > 1) {
                var num = that.data.time
                num--

                console.log(num)

                that.setData({
                  time: num
                })
                console.log(that.data.time);
              } else {
                that.setData({
                  telNum: true,
                  time_status: true,
                  sma_status: false
                })
                clearInterval(timer);
              }
            }, 1000);
          }
        }
      },
      err => {
        console.log(err);
      })


  },

  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })

    console.log(this.data);

    this.setData({
      telFlag: this.data.tel != '',
      smsFlag: this.data.sms != ''
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(wx.getStorageSync('saleUserId'));
    if (wx.getStorageSync('saleUserId') != '' || app.globalData.userId != 0) {
      app.globalData.userId = wx.getStorageSync('saleUserId')
      app.globalData.companyId = wx.getStorageSync('salecompanyId')
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})